import requests
import redis
import time

proxypool_url = 'http://124.223.11.108:5010/get'
target_url72 = 'http://72.19.12.227:80//link/U5t0Lhq3Kpfvvkvz?sub=3'
target_url = 'http://www.demobbs.top/api/v1/client/subscribe?token=45432aa217a23fe65a2a340002c5d5dd'

ip = '124.223.11.108'
password = 'lihao'

# conn_pool = redis.ConnectionPool(host=ip,password=password, port=6379)
# r = redis.Redis(connection_pool=conn_pool)
r=redis.Redis(host=ip,password=password,port=6379,db=0,decode_responses=True)#decode_responses=True 自动解码，输出的结果自动由bytes类型变为字符串类型

day1 = '17:30:20'
time.sleep(1)
day2 = time.strftime('%H:%M:%S',time.localtime(time.time()))
time.sleep(1)
day3 = time.strftime('%H:%M:%S',time.localtime(time.time()))


if day1 < day2 :
    print("true")
r.set('name','zhangsansan')
#r.hset("checksub","72",1)
#r.hset("checksub","demo",0)
flag = r.hget('checksub',"72")
print(r.hget('checksub',"72"))
print(r.get('name'))
def compare(time1,time2):
    time1Array = time1.split(":");
    time2Array = time2.split(":");
    if int(time1Array[0]) < int(time2Array[0]) :
        return True;
    else :
        if int(time1Array[0]) == int(time2Array[0]) :
            if int(time1Array[1]) < int(time2Array[1]):
                return True;
            else:
                if int(time1Array[1]) == int(time2Array[1]):
                    if int(time1Array[2]) < int(time2Array[2]) :
                        return True;
                    else:
                        return False;
                else:
                    return False;
        else:
            return False;

def get_random_proxy():
    """
    get random proxy from proxypool
    :return: proxy
    """
    return requests.get(proxypool_url).text.strip()


def crawl(url, proxy):
    """
    use proxy to crawl page
    :param url: page url
    :param proxy: proxy, such as 8.8.8.8:8888
    :return: html
    """
    proxies = {'https': 'http://' + proxy}
    return requests.get(url, proxies=proxies).text
def get_proxy():
    return requests.get(proxypool_url).json()

def delete_proxy(proxy):
    requests.get("http://124.223.11.108:5010/delete/?proxy={}".format(proxy))

# your spider code

def getHtml():
    # ....
    retry_count = 5
    proxy = get_proxy().get("proxy")
    while retry_count > 0:
        try:
            html = requests.get(target_url, proxies={"http": "http://{}".format(proxy)})
            # 使用代理访问
            print("检测demobbs.top")
            print(html.text)
            if html.status_code != 200 :
                r.hset("checksub", "demo", int(r.hget('checksub', "demo"))+1)
                #requests.get("http://124.223.11.108:9080/gUw5GwL3yRozyuTKCD2RBY/网站检测/网站订阅已挂！")
            else:
                r.hset("checksub", "demo", 0)
            return html
        except Exception:
            retry_count -= 1
    # 删除代理池中代理
    #delete_proxy(proxy)
    return None
def getHtml72():
    # ....
    retry_count = 5
    proxy = get_proxy().get("proxy")
    while retry_count > 0:
        try:
            html = requests.get(target_url72, proxies={"http": "http://{}".format(proxy)})
            # 使用代理访问
            print("检测72.19.12.227")
            print(html.text)
            if html.status_code != 200 :
                r.hset("checksub", "72", int(r.hget('checksub', "72"))+1)
                #requests.get("http://124.223.11.108:9080/gUw5GwL3yRozyuTKCD2RBY/网站检测/网站订阅已挂！")
            else:
                r.hset("checksub", "72", 0)
            return html
        except Exception:
            retry_count -= 1
    # 删除代理池中代理
    #delete_proxy(proxy)
    return None
def checkInfo():
    if int(r.hget('checksub', "72")) > 5 :
        requests.get("http://124.223.11.108:9080/gUw5GwL3yRozyuTKCD2RBY/网站检测/72网站订阅已挂！")
    if int(r.hget('checksub', "demo")) > 5:
        requests.get("http://124.223.11.108:9080/gUw5GwL3yRozyuTKCD2RBY/网站检测/demo网站订阅已挂！")
    print("校验结束")
    r.close()

getHtml()
getHtml72()
checkInfo()

if compare('8:15:20',time.strftime('%H:%M:%S',time.localtime(time.time())))==False and r.hget('checksub', "send") == "1":
    r.hset("checksub", "send", "0")
if compare('8:15:20',time.strftime('%H:%M:%S',time.localtime(time.time()))) and r.hget('checksub', "send") == "0":
    requests.get("http://124.223.11.108:9080/gUw5GwL3yRozyuTKCD2RBY/网站检测/每日巡逻检验正常！")
    r.hset("checksub", "send", "1")